const Router = require('koa-router')
const requireDirectory = require('require-directory');
class InitManager{
    static initCore(app) {
        InitManager.app = app;
        InitManager.initLoadRoutes();
        InitManager.loadHttpException();
        InitManager.loadConfig();
    }
    static initLoadRoutes() {
        const apiDirectory = `${process.cwd()}/app/api`;
        requireDirectory(module, apiDirectory, {
            visit: whenLoadModule
        });
        
        function whenLoadModule(obj) {
            if(obj instanceof Router) {
                InitManager.app.use(obj.routes())
            }
        }
    }

    static loadHttpException() {
        const error = require('./http-exception');
        global.errs = error;
    }

    static loadConfig(path = '') {
        const configPath = path || process.cwd() + '/config/config.js';
        const config = require(configPath);
        global.config = config;
    }
}

module.exports = InitManager;